package com.mini.demos.webgame.handler;

import java.util.List;

import com.mini.demos.webgame.db.JobService;
import com.mini.demos.webgame.entity.Job;
import com.mini.demos.webgame.msg.JobListMsg;
import com.mini.demos.webgame.msg.JobListMsgResp;
import com.mini.demos.webgame.msg.MsgHead;
import com.mini.demos.webgame.util.CommandConstants;
import com.mini.demos.webgame.util.MsgSequenceId;
import com.mini.demos.webgame.util.Utils;
import com.mini.demos.webgame.util.XmlUtils;
import com.mini.framework.communicate.mina.handler.Handler;
import com.mini.framework.communicate.mina.protocol.Msg;

public class JobListHandler extends Handler {

	@Override
	public void handle(Msg msg) {
		JobListMsg jobListMsg = (JobListMsg)msg;
		String userName = jobListMsg.getUserName();
		JobService jobService = new JobService();
		List<Job> jobs = jobService.getJobList(userName);
		String jobListXml = XmlUtils.transJobsXml(jobs);
		
		JobListMsgResp jobListMsgResp = new JobListMsgResp();
		jobListMsgResp.setCode(0);
		jobListMsgResp.setJobXmlBody(jobListXml);
		
		MsgHead head = new MsgHead();
		head.setVersion((byte) 0x01);
		head.setCommandId(CommandConstants.JOB_LIST_RESP);
		head.setSeqId(MsgSequenceId.getInstance().getSequenceId());
		
		Integer totalLength = CommandConstants.HEAD_LENGTH+ jobListMsgResp.getBodyLength();
		head.setTotalLength(totalLength.intValue());
		head.setTime(Utils.getTime());
		head.setSpace(0);
		jobListMsgResp.setHead(head);
		
		jobListMsg.getSender().send(jobListMsgResp);
	}

}
